package com.skali.portlet.governmentSpending.admin;

import java.io.IOException;
import java.util.Date;
import java.util.Locale;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.util.bridges.mvc.MVCPortlet;
import com.skali.portlet.governmentSpending.service.model.SpendingType;
import com.skali.portlet.governmentSpending.service.model.impl.SpendingTypeImpl;
import com.skali.portlet.governmentSpending.service.service.SpendingTypeLocalServiceUtil;

/**
 * Portlet implementation class GovernmentSpendingAdmin
 */
public class GovernmentSpendingAdmin extends MVCPortlet {

 
	public void spendingType(ActionRequest actionrequest, 
			ActionResponse actionResponse)throws IOException,SystemException,PortalException {
		System.out.println("am in action");
		String spendingTypeName = ParamUtil.getString(actionrequest,"spendingTypeName");
		String spendingTypeDesc = ParamUtil.getString(actionrequest,"spendingTypeDesc");
		boolean delType = ParamUtil.getBoolean(actionrequest,"spendingDelete");
		long editTypeId = ParamUtil.getLong(actionrequest, "edit");
		Locale locale = actionrequest.getLocale();
		if(Validator.isNotNull(editTypeId)){
			SpendingType editSpendType = SpendingTypeLocalServiceUtil.getSpendingType(editTypeId);
			editSpendType.setDescription(locale, spendingTypeDesc);
			editSpendType.setSpendingName(locale, spendingTypeName);
			editSpendType.setModifiedDate(new Date());
			SpendingTypeLocalServiceUtil.updateSpendingType(editSpendType);
		}else if(delType){
			SpendingType delSpendType = SpendingTypeLocalServiceUtil.getSpendingType(editTypeId);
			delSpendType.setDescription(locale, "");
			delSpendType.setSpendingName(locale, "");
			SpendingTypeLocalServiceUtil.deleteSpendingType(delSpendType);
		}else {
		
			SpendingType spendType = new SpendingTypeImpl();
			spendType.setSpendingName(locale, spendingTypeName);
			spendType.setDescription(locale, spendingTypeDesc);
			spendType.setModifiedDate(new Date());
			spendType.setCustomField(true);
			SpendingTypeLocalServiceUtil.addSpendingType(spendType);
		}
		System.out.println("name-->"+spendingTypeName+"--desc-->"+spendingTypeDesc);
		actionResponse.setRenderParameter("jspPage", "/html/portlet/governmentspending/admin/view.jsp");
		actionResponse.setRenderParameter("tabs1", "Spending-Type");
	}
	
	}
